using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.IO;
using System.IO.Ports;
using System.Net;
using System.Windows.Forms;

namespace ICP_MS.Plugin.Experiment;

public class FormLIMS : Form
{
	private IContainer components = null;

	private GroupBox groupBox1;

	private ListBox listBoxExprriment;

	private Button buttonScan;

	private TabControl tabControl1;

	private TabPage tabPage1;

	private TextBox txtPort;

	private Label label2;

	private TextBox txtIp;

	private Label label1;

	private TabPage tabPage2;

	private TextBox txtDatabits;

	private ComboBox cbStopBits;

	private Label label7;

	private Label label6;

	private ComboBox cbParity;

	private Label label5;

	private ComboBox cbBaudRate;

	private Label label4;

	private ComboBox cbPort;

	private Label label3;

	private GroupBox groupBox2;

	private RadioButton radioButtonRS232;

	private RadioButton radioButtonTCP;

	private RadioButton radioButtonUDP;

	private Button btnCancel;

	private Button btnOk;

	private GroupBox groupBox3;

	private TextBox txtDir;

	private Button btnDir;

	public string HostIP { get; set; }

	public int HostPort { get; set; }

	public SerialPort SerialPort { get; set; }

	public int LimsType { get; set; }

	public List<string> fileNameList { get; set; }

	public string fileName { get; set; }

	public string FileDir { get; set; }

	public FormLIMS()
	{
		InitializeComponent();
	}

	private void FormConfig_Load(object sender, EventArgs e)
	{
		ComboBox.ObjectCollection items = cbPort.Items;
		object[] portNames = SerialPort.GetPortNames();
		items.AddRange(portNames);
		ComboBox.ObjectCollection items2 = cbParity.Items;
		portNames = Enum.GetNames(typeof(Parity));
		items2.AddRange(portNames);
		ComboBox.ObjectCollection items3 = cbBaudRate.Items;
		portNames = new string[12]
		{
			"300", "600", "1200", "2400", "4800", "9600", "19200", "38400", "43000", "56000",
			"57600", "115200"
		};
		items3.AddRange(portNames);
		ComboBox.ObjectCollection items4 = cbStopBits.Items;
		portNames = Enum.GetNames(typeof(StopBits));
		items4.AddRange(portNames);
		if (cbPort.Items.Count >= 0)
		{
			cbPort.SelectedIndex = 0;
		}
		cbParity.SelectedIndex = 0;
		cbStopBits.SelectedIndex = 0;
		cbBaudRate.SelectedItem = "9600";
		txtIp.Text = HostIP;
		txtPort.Text = HostPort.ToString();
		txtDir.Text = FileDir;
		try
		{
			txtDatabits.Text = SerialPort.DataBits.ToString();
			cbParity.SelectedItem = SerialPort.Parity.ToString();
			cbStopBits.SelectedItem = SerialPort.StopBits.ToString();
			cbPort.SelectedItem = SerialPort.PortName;
			cbBaudRate.SelectedItem = SerialPort.BaudRate.ToString();
		}
		catch
		{
		}
		radioButtonTCP.Checked = LimsType == 0;
		radioButtonUDP.Checked = LimsType == 1;
		radioButtonRS232.Checked = LimsType == 2;
		listBoxExprriment.Items.Clear();
		foreach (string fileName in fileNameList)
		{
			listBoxExprriment.Items.Add(Path.GetFileNameWithoutExtension(fileName));
		}
	}

	private void ExportLimsTypeChanged(object sender, EventArgs e)
	{
		if (radioButtonTCP.Checked)
		{
			LimsType = 0;
		}
		if (radioButtonUDP.Checked)
		{
			LimsType = 1;
		}
		if (radioButtonRS232.Checked)
		{
			LimsType = 2;
		}
	}

	private void buttonScan_Click(object sender, EventArgs e)
	{
		OpenFileDialog openFileDialog = new OpenFileDialog();
		openFileDialog.Filter = "IMF文件(*.imf)|*.imf|ICP数据文件(*.imdb)|*.imdb";
		openFileDialog.DefaultExt = "*.imf";
		openFileDialog.Multiselect = false;
		if (openFileDialog.ShowDialog() == DialogResult.OK)
		{
			fileNameList.Add(openFileDialog.FileName);
			listBoxExprriment.Items.Add(Path.GetFileNameWithoutExtension(openFileDialog.FileName));
		}
	}

	private void btnOk_Click(object sender, EventArgs e)
	{
		try
		{
			IPAddress ip = IPAddress.Parse(txtIp.Text);
			HostIP = txtIp.Text;
		}
		catch
		{
			MessageBox.Show(Lang.Txt("AddFormLIMS.IPError"));
			return;
		}
		try
		{
			int port = Convert.ToInt32(txtPort.Text);
			if (port <= 0 || port > 65535)
			{
				throw new Exception();
			}
			HostPort = port;
		}
		catch
		{
			MessageBox.Show(Lang.Txt("AddFormLIMS.TCPError"));
			return;
		}
		try
		{
			SerialPort.PortName = cbPort.SelectedItem.ToString();
			SerialPort.Parity = (Parity)Enum.Parse(typeof(Parity), cbParity.SelectedItem.ToString());
			SerialPort.BaudRate = Convert.ToInt32(cbBaudRate.SelectedItem.ToString());
			SerialPort.DataBits = Convert.ToInt32(txtDatabits.Text);
			SerialPort.StopBits = (StopBits)Enum.Parse(typeof(StopBits), cbStopBits.SelectedItem.ToString());
		}
		catch
		{
			MessageBox.Show(Lang.Txt("AddFormLIMS.PortWrong"));
			return;
		}
		if (listBoxExprriment.SelectedItems.Count > 0)
		{
			fileName = fileNameList[listBoxExprriment.SelectedIndex];
			base.DialogResult = DialogResult.OK;
		}
		else
		{
			MessageBox.Show(Lang.Txt("AddFormLIMS.ChooseMethod"), Lang.Txt("AddFormLIMS.Warn"), MessageBoxButtons.OK);
		}
	}

	private void btnCancel_Click(object sender, EventArgs e)
	{
		base.DialogResult = DialogResult.Cancel;
	}

	private void btnDir_Click(object sender, EventArgs e)
	{
		FolderBrowserDialog dlg = new FolderBrowserDialog();
		try
		{
			dlg.SelectedPath = txtDir.Text;
		}
		catch
		{
		}
		if (dlg.ShowDialog() == DialogResult.OK)
		{
			txtDir.Text = dlg.SelectedPath;
		}
	}

	protected override void Dispose(bool disposing)
	{
		if (disposing && components != null)
		{
			components.Dispose();
		}
		base.Dispose(disposing);
	}

	private void InitializeComponent()
	{
		this.groupBox1 = new System.Windows.Forms.GroupBox();
		this.buttonScan = new System.Windows.Forms.Button();
		this.listBoxExprriment = new System.Windows.Forms.ListBox();
		this.tabControl1 = new System.Windows.Forms.TabControl();
		this.tabPage1 = new System.Windows.Forms.TabPage();
		this.txtPort = new System.Windows.Forms.TextBox();
		this.label2 = new System.Windows.Forms.Label();
		this.txtIp = new System.Windows.Forms.TextBox();
		this.label1 = new System.Windows.Forms.Label();
		this.tabPage2 = new System.Windows.Forms.TabPage();
		this.txtDatabits = new System.Windows.Forms.TextBox();
		this.cbStopBits = new System.Windows.Forms.ComboBox();
		this.label7 = new System.Windows.Forms.Label();
		this.label6 = new System.Windows.Forms.Label();
		this.cbParity = new System.Windows.Forms.ComboBox();
		this.label5 = new System.Windows.Forms.Label();
		this.cbBaudRate = new System.Windows.Forms.ComboBox();
		this.label4 = new System.Windows.Forms.Label();
		this.cbPort = new System.Windows.Forms.ComboBox();
		this.label3 = new System.Windows.Forms.Label();
		this.groupBox2 = new System.Windows.Forms.GroupBox();
		this.radioButtonRS232 = new System.Windows.Forms.RadioButton();
		this.radioButtonTCP = new System.Windows.Forms.RadioButton();
		this.radioButtonUDP = new System.Windows.Forms.RadioButton();
		this.btnCancel = new System.Windows.Forms.Button();
		this.btnOk = new System.Windows.Forms.Button();
		this.groupBox3 = new System.Windows.Forms.GroupBox();
		this.txtDir = new System.Windows.Forms.TextBox();
		this.btnDir = new System.Windows.Forms.Button();
		this.groupBox1.SuspendLayout();
		this.tabControl1.SuspendLayout();
		this.tabPage1.SuspendLayout();
		this.tabPage2.SuspendLayout();
		this.groupBox2.SuspendLayout();
		this.groupBox3.SuspendLayout();
		base.SuspendLayout();
		this.groupBox1.Controls.Add(this.buttonScan);
		this.groupBox1.Controls.Add(this.listBoxExprriment);
		this.groupBox1.Location = new System.Drawing.Point(12, 12);
		this.groupBox1.Name = "groupBox1";
		this.groupBox1.Size = new System.Drawing.Size(195, 220);
		this.groupBox1.TabIndex = 1;
		this.groupBox1.TabStop = false;
		this.groupBox1.Text = "选择一个方法";
		this.buttonScan.Location = new System.Drawing.Point(205, 72);
		this.buttonScan.Name = "buttonScan";
		this.buttonScan.Size = new System.Drawing.Size(57, 81);
		this.buttonScan.TabIndex = 6;
		this.buttonScan.Text = "浏览";
		this.buttonScan.UseVisualStyleBackColor = true;
		this.buttonScan.Visible = false;
		this.buttonScan.Click += new System.EventHandler(buttonScan_Click);
		this.listBoxExprriment.Dock = System.Windows.Forms.DockStyle.Left;
		this.listBoxExprriment.FormattingEnabled = true;
		this.listBoxExprriment.ItemHeight = 12;
		this.listBoxExprriment.Location = new System.Drawing.Point(3, 17);
		this.listBoxExprriment.Name = "listBoxExprriment";
		this.listBoxExprriment.Size = new System.Drawing.Size(185, 200);
		this.listBoxExprriment.TabIndex = 2;
		this.tabControl1.Controls.Add(this.tabPage1);
		this.tabControl1.Controls.Add(this.tabPage2);
		this.tabControl1.Location = new System.Drawing.Point(78, 17);
		this.tabControl1.Name = "tabControl1";
		this.tabControl1.SelectedIndex = 0;
		this.tabControl1.Size = new System.Drawing.Size(266, 191);
		this.tabControl1.TabIndex = 2;
		this.tabPage1.BackColor = System.Drawing.SystemColors.Control;
		this.tabPage1.Controls.Add(this.txtPort);
		this.tabPage1.Controls.Add(this.label2);
		this.tabPage1.Controls.Add(this.txtIp);
		this.tabPage1.Controls.Add(this.label1);
		this.tabPage1.Location = new System.Drawing.Point(4, 22);
		this.tabPage1.Name = "tabPage1";
		this.tabPage1.Padding = new System.Windows.Forms.Padding(3);
		this.tabPage1.Size = new System.Drawing.Size(258, 165);
		this.tabPage1.TabIndex = 0;
		this.tabPage1.Text = "Tcp";
		this.txtPort.Location = new System.Drawing.Point(51, 100);
		this.txtPort.Name = "txtPort";
		this.txtPort.Size = new System.Drawing.Size(171, 21);
		this.txtPort.TabIndex = 3;
		this.txtPort.Text = "8001";
		this.label2.AutoSize = true;
		this.label2.Location = new System.Drawing.Point(27, 85);
		this.label2.Name = "label2";
		this.label2.Size = new System.Drawing.Size(35, 12);
		this.label2.TabIndex = 2;
		this.label2.Text = "端口:";
		this.txtIp.Location = new System.Drawing.Point(51, 46);
		this.txtIp.Name = "txtIp";
		this.txtIp.Size = new System.Drawing.Size(171, 21);
		this.txtIp.TabIndex = 1;
		this.txtIp.Text = "192.168.1.100";
		this.label1.AutoSize = true;
		this.label1.Location = new System.Drawing.Point(15, 31);
		this.label1.Name = "label1";
		this.label1.Size = new System.Drawing.Size(47, 12);
		this.label1.TabIndex = 0;
		this.label1.Text = "IP地址:";
		this.tabPage2.BackColor = System.Drawing.SystemColors.Control;
		this.tabPage2.Controls.Add(this.txtDatabits);
		this.tabPage2.Controls.Add(this.cbStopBits);
		this.tabPage2.Controls.Add(this.label7);
		this.tabPage2.Controls.Add(this.label6);
		this.tabPage2.Controls.Add(this.cbParity);
		this.tabPage2.Controls.Add(this.label5);
		this.tabPage2.Controls.Add(this.cbBaudRate);
		this.tabPage2.Controls.Add(this.label4);
		this.tabPage2.Controls.Add(this.cbPort);
		this.tabPage2.Controls.Add(this.label3);
		this.tabPage2.Location = new System.Drawing.Point(4, 22);
		this.tabPage2.Name = "tabPage2";
		this.tabPage2.Padding = new System.Windows.Forms.Padding(3);
		this.tabPage2.Size = new System.Drawing.Size(258, 165);
		this.tabPage2.TabIndex = 1;
		this.tabPage2.Text = "RS232";
		this.txtDatabits.Location = new System.Drawing.Point(104, 95);
		this.txtDatabits.Name = "txtDatabits";
		this.txtDatabits.Size = new System.Drawing.Size(100, 21);
		this.txtDatabits.TabIndex = 10;
		this.cbStopBits.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
		this.cbStopBits.FormattingEnabled = true;
		this.cbStopBits.Location = new System.Drawing.Point(104, 122);
		this.cbStopBits.Name = "cbStopBits";
		this.cbStopBits.Size = new System.Drawing.Size(100, 20);
		this.cbStopBits.TabIndex = 9;
		this.label7.AutoSize = true;
		this.label7.Location = new System.Drawing.Point(42, 125);
		this.label7.Name = "label7";
		this.label7.Size = new System.Drawing.Size(47, 12);
		this.label7.TabIndex = 8;
		this.label7.Text = "停止位:";
		this.label6.AutoSize = true;
		this.label6.Location = new System.Drawing.Point(42, 100);
		this.label6.Name = "label6";
		this.label6.Size = new System.Drawing.Size(47, 12);
		this.label6.TabIndex = 6;
		this.label6.Text = "数据位:";
		this.cbParity.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
		this.cbParity.FormattingEnabled = true;
		this.cbParity.Location = new System.Drawing.Point(104, 69);
		this.cbParity.Name = "cbParity";
		this.cbParity.Size = new System.Drawing.Size(100, 20);
		this.cbParity.TabIndex = 5;
		this.label5.AutoSize = true;
		this.label5.Location = new System.Drawing.Point(54, 72);
		this.label5.Name = "label5";
		this.label5.Size = new System.Drawing.Size(35, 12);
		this.label5.TabIndex = 4;
		this.label5.Text = "校验:";
		this.cbBaudRate.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
		this.cbBaudRate.FormattingEnabled = true;
		this.cbBaudRate.Location = new System.Drawing.Point(104, 43);
		this.cbBaudRate.Name = "cbBaudRate";
		this.cbBaudRate.Size = new System.Drawing.Size(100, 20);
		this.cbBaudRate.TabIndex = 3;
		this.label4.AutoSize = true;
		this.label4.Location = new System.Drawing.Point(42, 45);
		this.label4.Name = "label4";
		this.label4.Size = new System.Drawing.Size(47, 12);
		this.label4.TabIndex = 2;
		this.label4.Text = "波特率:";
		this.cbPort.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
		this.cbPort.FormattingEnabled = true;
		this.cbPort.Location = new System.Drawing.Point(104, 17);
		this.cbPort.Name = "cbPort";
		this.cbPort.Size = new System.Drawing.Size(100, 20);
		this.cbPort.TabIndex = 1;
		this.label3.AutoSize = true;
		this.label3.Location = new System.Drawing.Point(54, 20);
		this.label3.Name = "label3";
		this.label3.Size = new System.Drawing.Size(35, 12);
		this.label3.TabIndex = 0;
		this.label3.Text = "端口:";
		this.groupBox2.Controls.Add(this.radioButtonRS232);
		this.groupBox2.Controls.Add(this.radioButtonTCP);
		this.groupBox2.Controls.Add(this.radioButtonUDP);
		this.groupBox2.Controls.Add(this.tabControl1);
		this.groupBox2.Location = new System.Drawing.Point(217, 12);
		this.groupBox2.Name = "groupBox2";
		this.groupBox2.Size = new System.Drawing.Size(350, 220);
		this.groupBox2.TabIndex = 15;
		this.groupBox2.TabStop = false;
		this.groupBox2.Text = "选择传输方式";
		this.radioButtonRS232.AutoSize = true;
		this.radioButtonRS232.Location = new System.Drawing.Point(8, 141);
		this.radioButtonRS232.Name = "radioButtonRS232";
		this.radioButtonRS232.Size = new System.Drawing.Size(53, 16);
		this.radioButtonRS232.TabIndex = 17;
		this.radioButtonRS232.Text = "RS232";
		this.radioButtonRS232.UseVisualStyleBackColor = true;
		this.radioButtonRS232.CheckedChanged += new System.EventHandler(ExportLimsTypeChanged);
		this.radioButtonTCP.AutoSize = true;
		this.radioButtonTCP.Checked = true;
		this.radioButtonTCP.Location = new System.Drawing.Point(8, 39);
		this.radioButtonTCP.Name = "radioButtonTCP";
		this.radioButtonTCP.Size = new System.Drawing.Size(41, 16);
		this.radioButtonTCP.TabIndex = 18;
		this.radioButtonTCP.TabStop = true;
		this.radioButtonTCP.Text = "TCP";
		this.radioButtonTCP.UseVisualStyleBackColor = true;
		this.radioButtonTCP.CheckedChanged += new System.EventHandler(ExportLimsTypeChanged);
		this.radioButtonUDP.AutoSize = true;
		this.radioButtonUDP.Location = new System.Drawing.Point(8, 90);
		this.radioButtonUDP.Name = "radioButtonUDP";
		this.radioButtonUDP.Size = new System.Drawing.Size(41, 16);
		this.radioButtonUDP.TabIndex = 16;
		this.radioButtonUDP.Text = "UDP";
		this.radioButtonUDP.UseVisualStyleBackColor = true;
		this.radioButtonUDP.Visible = false;
		this.radioButtonUDP.CheckedChanged += new System.EventHandler(ExportLimsTypeChanged);
		this.btnCancel.Location = new System.Drawing.Point(466, 257);
		this.btnCancel.Name = "btnCancel";
		this.btnCancel.Size = new System.Drawing.Size(75, 23);
		this.btnCancel.TabIndex = 17;
		this.btnCancel.Text = "取消";
		this.btnCancel.UseVisualStyleBackColor = true;
		this.btnCancel.Click += new System.EventHandler(btnCancel_Click);
		this.btnOk.Location = new System.Drawing.Point(339, 257);
		this.btnOk.Name = "btnOk";
		this.btnOk.Size = new System.Drawing.Size(75, 23);
		this.btnOk.TabIndex = 16;
		this.btnOk.Text = "确定";
		this.btnOk.UseVisualStyleBackColor = true;
		this.btnOk.Click += new System.EventHandler(btnOk_Click);
		this.groupBox3.Controls.Add(this.txtDir);
		this.groupBox3.Controls.Add(this.btnDir);
		this.groupBox3.Location = new System.Drawing.Point(15, 239);
		this.groupBox3.Name = "groupBox3";
		this.groupBox3.Size = new System.Drawing.Size(274, 51);
		this.groupBox3.TabIndex = 18;
		this.groupBox3.TabStop = false;
		this.groupBox3.Text = "LIMS文件路径";
		this.txtDir.Location = new System.Drawing.Point(16, 20);
		this.txtDir.Name = "txtDir";
		this.txtDir.Size = new System.Drawing.Size(153, 21);
		this.txtDir.TabIndex = 12;
		this.txtDir.Text = "D:\\LIMS";
		this.btnDir.Location = new System.Drawing.Point(197, 22);
		this.btnDir.Name = "btnDir";
		this.btnDir.Size = new System.Drawing.Size(62, 23);
		this.btnDir.TabIndex = 13;
		this.btnDir.Text = "选择..";
		this.btnDir.UseVisualStyleBackColor = true;
		this.btnDir.Click += new System.EventHandler(btnDir_Click);
		base.AutoScaleDimensions = new System.Drawing.SizeF(6f, 12f);
		base.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
		base.ClientSize = new System.Drawing.Size(577, 302);
		base.Controls.Add(this.groupBox3);
		base.Controls.Add(this.btnCancel);
		base.Controls.Add(this.btnOk);
		base.Controls.Add(this.groupBox2);
		base.Controls.Add(this.groupBox1);
		base.Name = "FormConfig";
		this.Text = "配置";
		base.Load += new System.EventHandler(FormConfig_Load);
		this.groupBox1.ResumeLayout(false);
		this.tabControl1.ResumeLayout(false);
		this.tabPage1.ResumeLayout(false);
		this.tabPage1.PerformLayout();
		this.tabPage2.ResumeLayout(false);
		this.tabPage2.PerformLayout();
		this.groupBox2.ResumeLayout(false);
		this.groupBox2.PerformLayout();
		this.groupBox3.ResumeLayout(false);
		this.groupBox3.PerformLayout();
		base.ResumeLayout(false);
	}
}
